package example.ble;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.administrator.myapplication.R;

import java.util.Random;

public class MySurface extends SurfaceView implements SurfaceHolder.Callback {

    private Paint paint;
    private Paint imgPaint;
    private GraphThread thread = null;
    private int MaxX, MaxY;

    private Matrix mMatrix;
    private Bitmap mRocket;

    private float mRocketRotate = 0f;
    private int mRocketX = 0;
    private int mRocketY = 0;
    private double mRocketSpeed = 0.0 , mRocketRotateSpeed = 0.0 ;

    public void setMove(double v) {
        mRocketSpeed = v;
    }
    public void setRotate(double v) {
        mRocketRotateSpeed = v;
    }

    public MySurface(Context c, AttributeSet attr){
        super(c, attr);
        paint = new Paint();

        imgPaint = new Paint();
        paint.setColor(Color.RED);
        getHolder().addCallback(this);
        mRocket = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
        mMatrix = new Matrix();
        setWillNotDraw(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mRocketRotate += mRocketRotateSpeed;
        if(mRocketRotate >= 360.0f) {
            mRocketRotate = 0.0f;
        }

        mMatrix.setTranslate(0,0);
        mMatrix.setRotate(mRocketRotate, mRocket.getWidth()/2, mRocket.getHeight()/2);
        if(mRocketRotate<=90) {
            mRocketX += ( (int) (Math.sin(Math.toRadians(mRocketRotate)) * mRocketSpeed * 200) )/100;
            mRocketY -= ( (int) (Math.cos(Math.toRadians(mRocketRotate)) * mRocketSpeed * 200) )/100;
        } else if(mRocketRotate > 90 && mRocketRotate<=180) {
            mRocketX += ( (int) (Math.sin(Math.toRadians(180-mRocketRotate)) * mRocketSpeed * 200) )/100;
            mRocketY += ( (int) (Math.cos(Math.toRadians(180-mRocketRotate)) * mRocketSpeed * 200) )/100;
        } else if(mRocketRotate > 180 && mRocketRotate<=270) {
            mRocketX -= ( (int) (Math.cos(Math.toRadians(270-mRocketRotate)) * mRocketSpeed * 200) )/100;
            mRocketY += ( (int) (Math.sin(Math.toRadians(270-mRocketRotate)) * mRocketSpeed * 200) )/100;
        } else {
            mRocketX -= ( (int) (Math.sin(Math.toRadians(360-mRocketRotate)) * mRocketSpeed * 200) )/100;
            mRocketY -= ( (int) (Math.cos(Math.toRadians(360-mRocketRotate)) * mRocketSpeed * 200) )/100;
        }
        if(mRocketX < -1*mRocket.getWidth()/2)
            mRocketX = -1*mRocket.getWidth()/2;
        else if(mRocketX > MaxX-mRocket.getWidth()/2)
            mRocketX = MaxX-mRocket.getWidth()/2;

        if(mRocketY < -1*mRocket.getHeight()/2)
            mRocketY = -1*mRocket.getHeight()/2;
        else if(mRocketY > MaxY-mRocket.getHeight()/2)
            mRocketY = MaxY-mRocket.getHeight()/2;

        mMatrix.postTranslate(mRocketX, mRocketY);
        canvas.drawBitmap(mRocket, mMatrix, imgPaint);
    }

    public void update()
    {
        postInvalidate();
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        MaxX = getWidth();
        MaxY = getHeight();

        thread = new GraphThread( this );
        thread.setRun();
        thread.start();

        mRocketX = (MaxX-mRocket.getWidth())/2;
        mRocketY = 2*MaxY/3;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if(thread != null)
            thread.setPause();
    }
}
